{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "afff5863-f321-440d-a622-3b39ba578a94",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-13T17:40:51.408926Z",
     "iopub.status.busy": "2022-02-13T17:40:51.408635Z",
     "iopub.status.idle": "2022-02-13T17:40:52.043512Z",
     "shell.execute_reply": "2022-02-13T17:40:52.042716Z",
     "shell.execute_reply.started": "2022-02-13T17:40:51.408856Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b607fbbb-5200-43b7-9705-e923f472d3e2",
   "metadata": {},
   "source": [
    "1. hsplit-水平方向切割，即按列进行切割\n",
    "    1. 第一种切割方式：直接指定均匀切割成多少列\n",
    "    2. 第二种切割防止：指定切割列的下标值\n",
    "2. vsplit-垂直方向切割，即按行进行切割\n",
    "    1. 第一种切割方式：直接指定均匀切割成多少行\n",
    "    2. 第二种切割防止：指定切割行的下标值\n",
    "3. split/array_split-手动指定切割方向（axis=）\n",
    "    1. axis=0，垂直方向，即按行进行切割\n",
    "    2. axis=1，水平方向，即按列进行切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1ff0341a-df4a-4122-b85a-e48fedd886e0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-13T17:50:32.737879Z",
     "iopub.status.busy": "2022-02-13T17:50:32.737646Z",
     "iopub.status.idle": "2022-02-13T17:50:32.746742Z",
     "shell.execute_reply": "2022-02-13T17:50:32.745958Z",
     "shell.execute_reply.started": "2022-02-13T17:50:32.737855Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[1 1 0 3 9 7]\n",
      " [4 3 6 1 1 0]\n",
      " [0 5 2 0 4 1]]\n",
      "====================\n",
      "np.hsplit(a1,2):\n",
      "[array([[1, 1, 0],\n",
      "       [4, 3, 6],\n",
      "       [0, 5, 2]]), array([[3, 9, 7],\n",
      "       [1, 1, 0],\n",
      "       [0, 4, 1]])]\n",
      "====================\n",
      "np.hsplit(a1,(1，2)):\n",
      "[array([[1],\n",
      "       [4],\n",
      "       [0]]), array([[1],\n",
      "       [3],\n",
      "       [5]]), array([[0, 3, 9, 7],\n",
      "       [6, 1, 1, 0],\n",
      "       [2, 0, 4, 1]])]\n"
     ]
    }
   ],
   "source": [
    "# 水平方向的切割hsplit(按列切割)\n",
    "a1 = np.random.randint(0, 10, size=(3, 6))  # 创建3行6列的数组a1，其中元素为0到9之间的随机整数\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"np.hsplit(a1,2):\\n{}\".format(np.hsplit(a1,2))) # 水平方向均匀切割成2份\n",
    "print(\"=\" * 20)\n",
    "print(\"np.hsplit(a1,(1，2)):\\n{}\".format(np.hsplit(a1,(1, 2)))) # 水平方向分别从列的第1个位置和第2个位置进行切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "941ef8db-d6d8-4151-a2cd-3e6a2e760f66",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-13T17:57:25.732387Z",
     "iopub.status.busy": "2022-02-13T17:57:25.732149Z",
     "iopub.status.idle": "2022-02-13T17:57:25.738067Z",
     "shell.execute_reply": "2022-02-13T17:57:25.737443Z",
     "shell.execute_reply.started": "2022-02-13T17:57:25.732362Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a2:\n",
      "[[5 7 0]\n",
      " [5 9 9]\n",
      " [3 2 8]\n",
      " [3 8 1]\n",
      " [2 8 8]\n",
      " [8 1 8]]\n",
      "====================\n",
      "np.vsplit(a2,3):\n",
      "[array([[5, 7, 0],\n",
      "       [5, 9, 9]]), array([[3, 2, 8],\n",
      "       [3, 8, 1]]), array([[2, 8, 8],\n",
      "       [8, 1, 8]])]\n",
      "====================\n",
      "np.vsplit(a2,(1,4)):\n",
      "[array([[5, 7, 0]]), array([[5, 9, 9],\n",
      "       [3, 2, 8],\n",
      "       [3, 8, 1]]), array([[2, 8, 8],\n",
      "       [8, 1, 8]])]\n"
     ]
    }
   ],
   "source": [
    "# 垂直方向的切割vsplit(按行切割)\n",
    "a2 = np.random.randint(0, 10, size=(6, 3))  # 创建3行6列的数组a1，其中元素为0到9之间的随机整数\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "print(\"np.vsplit(a2,3):\\n{}\".format(np.vsplit(a2,3))) # 垂直方向均匀切割成3份\n",
    "print(\"=\" * 20)\n",
    "print(\"np.vsplit(a2,(1,4)):\\n{}\".format(np.vsplit(a2,(1,4)))) # 垂直方向分别从行的第1个位置和第4个位置切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "af2cfbf2-3c64-45aa-b78c-2a59cb1dc880",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-13T18:07:50.176747Z",
     "iopub.status.busy": "2022-02-13T18:07:50.176505Z",
     "iopub.status.idle": "2022-02-13T18:07:50.182198Z",
     "shell.execute_reply": "2022-02-13T18:07:50.181346Z",
     "shell.execute_reply.started": "2022-02-13T18:07:50.176722Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[1 1 0 3 9 7]\n",
      " [4 3 6 1 1 0]\n",
      " [0 5 2 0 4 1]]\n",
      "====================\n",
      ":\n",
      "[array([[1, 1],\n",
      "       [4, 3],\n",
      "       [0, 5]]), array([[0, 3],\n",
      "       [6, 1],\n",
      "       [2, 0]]), array([[9, 7],\n",
      "       [1, 0],\n",
      "       [4, 1]])]\n"
     ]
    }
   ],
   "source": [
    "# 手动指定叠加方向(水平，按列切割)\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\":\\n{}\".format(np.split(a1,3,axis=1))) # 水平方向（axis=1）切成均等3份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2b790d9c-2032-44b6-893b-7324d3592e4e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-13T18:10:01.246665Z",
     "iopub.status.busy": "2022-02-13T18:10:01.246414Z",
     "iopub.status.idle": "2022-02-13T18:10:01.251628Z",
     "shell.execute_reply": "2022-02-13T18:10:01.250869Z",
     "shell.execute_reply.started": "2022-02-13T18:10:01.246640Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a2:\n",
      "[[5 7 0]\n",
      " [5 9 9]\n",
      " [3 2 8]\n",
      " [3 8 1]\n",
      " [2 8 8]\n",
      " [8 1 8]]\n",
      "====================\n",
      ":\n",
      "[array([[5, 7, 0],\n",
      "       [5, 9, 9]]), array([[3, 2, 8],\n",
      "       [3, 8, 1]]), array([[2, 8, 8],\n",
      "       [8, 1, 8]])]\n"
     ]
    }
   ],
   "source": [
    "# 手动指定叠加方向(垂直，按行切割)\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "print(\":\\n{}\".format(np.split(a2,3,axis=0))) # 垂直方向（axis=0）切成均等3份"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
